from typing import List


class Solution:
    def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:
       n = len(points)

       best, bestid = float("inf"),-1
       for i,(px,py) in enumerate(points):
           if px == x:
               if best > abs(py - y):
                   best = abs(py - y)
                   bestid = i
           if py == y:
               if best > abs(px -x):
                   best = abs(px - x)
                   bestid = i

       return bestid


if __name__ == '__main__':
    solution = Solution()
    x = 3
    y = 4
    points = [[1,2],[3,1],[2,4],[2,3],[4,4]]
    val = solution.nearestValidPoint(x,y,points)
    print(val)